package com.androidbook.garbagedemo1.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.androidbook.garbagedemo1.R;
import com.androidbook.garbagedemo1.api.Api;
import com.androidbook.garbagedemo1.api.ApiConfig;
import com.androidbook.garbagedemo1.api.TtitCallback;
import com.androidbook.garbagedemo1.utils.StringUtils;

import java.util.HashMap;

public class RegisterActivity extends BaseActivity {
    private EditText etAccount,etPwd;
    private Button btnRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        etAccount=findViewById(R.id.et_account);
        etPwd=findViewById(R.id.et_pwd);
        btnRegister=findViewById(R.id.btn_register);
        //绑定点击事件
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取输入框输入的用户名及密码
                String account=etAccount.getText().toString().trim();//getText()获取输入框输入的文本，toString()文本类型转换，trim()去掉文本前后空格
                String pwd=etPwd.getText().toString().trim();
                register(account,pwd);
            }
        });
    }


    private void register(String account,String pwd){
        //调用StringUtils.isEmpty的方法判断账号密码是否为空
        if (StringUtils.isEmpty(account)){
            showToast("请输入账号");//（调用基类BaseActivity的封装方法）账号为空，弹出提示框
            return;
        }
        if (StringUtils.isEmpty(pwd)){
            showToast("请输入密码");
            return;
        }
        HashMap<String,Object> params = new HashMap<String,Object>();
        params.put ( "mobile", account) ;
        params.put( "password" , pwd) ;
        Api.config(ApiConfig.REGISTER, params).postRequest(new TtitCallback() {
            @Override
            public void onSuccess(String res) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showToast(res);
                    }
                });
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure",e.toString());
            }
        });

    }
}